$(function () {
    $(document).bind("contextmenu", function (e) {
        return false;
    });

    var dict_dialog = $("#dict_dialog");
    var dict_form = $("#dict_form");
    var word_list = $("#dictWord");
    var menu_list = $("#dictMenu");
    var menu_id = 1;
    var menubelong;

    dict_dialog.dialog({
        closed: true,
        width: 300,
        height: 280,
        buttons: $("#dict_diaButtons"),
    })

    menu_list.datalist({
        url: '/dictionary/queryMenu.do',
        fit: true,
        title: '分类',
        lines: true,
        rownumbers: true,
        textField: 'name',
        valueField: 'id',
        footer: '#ft',
        onClickRow: function (index, row) {
            menu_id = row.id;
            $("#dictWord").datagrid({
                url: '/dictionary/queryWords.do?id=' + menu_id,
            })
        },
        onRowContextMenu: function (e, index, row) {
            menubelong = menu_list;
            $("#dictMenu").datagrid('selectRow', index);
            $("#sub_menu").menu('show',
                {
                    left: e.pageX,
                    top: e.pageY
                });
            menu_id = row.id;
            $("#dictWord").datagrid({
                url: '/dictionary/queryWords.do?id=' + menu_id,
            })
        }
    })

    word_list.datagrid({
        fit: true,
        rownumbers: true,
        pageSize: 15,
        pageList: [10, 15, 20, 30],
        onRowContextMenu: function (e, index, row) {
            menubelong = word_list;
            $("#dictWord").datagrid('selectRow', index);
            $("#sub_menu").menu('show',
                {
                    left: e.pageX,
                    top: e.pageY
                });
        },
        toolbar: [{
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                dict_dialog.dialog('setTitle', '添加词条');
                dict_dialog.css('height', 200);
                $("#dict_menu").show();
                dict_form.form('clear');
                dict_dialog.dialog('open');
            }
        }, '-', {
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function () {
                var row = word_list.datagrid('getSelected');
                if (row == null) {
                    $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                } else {
                    dict_form.form('clear');
                    $("#dict_menu").show();
                    dict_dialog.css('height', 200);
                    row['parentId'] = menu_id;
                    dict_form.form('load', row);
                    dict_dialog.dialog('setTitle', '编辑词条');
                    dict_dialog.dialog('open');
                }
            }
        }, '-', {
            text: '删除',
            iconCls: 'icon-cancel',
            handler: function () {
                var row = word_list.datagrid('getSelected');
                if (row == null) {
                    $.messager.alert('温馨提示', '请选择一条数据!', 'info');
                } else {
                    $.messager.confirm('温馨提示', '您确认想要删除吗？', function (r) {
                            if (r) {
                                $.get('/dictionary/deleteWord.do', {id: row.id}, function (data) {
                                    if (data.success) {
                                        $.messager.alert('温馨提示', data.msg, 'info', function () {
                                            dict_dialog.dialog('close');
                                            word_list.datagrid('reload');
                                        });
                                    } else {
                                        $.messager.alert('温馨提示', data.msg, 'error');
                                    }
                                }, 'json')
                            }
                        }
                    )
                }
            }
        }, '-', {
            text: '刷新',
            iconCls: 'icon-reload',
            handler: function () {
                $("#dictWord").datagrid('reload');
            }
        }]
        ,
        url: '/dictionary/queryWords.do?id=1',
        pagination: true,
        fitColumns: true,
        singleSelect: true,
        columns: [[
            {field: 'sn', title: '代码', width: 100},
            {field: 'name', title: '名称', width: 100},
            {field: 'text', title: '描述', width: 300}
        ]]
    })


    /*事件统一绑定*/
    $("[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        console.log(cmd);
        object[cmd]();
    })

    var object = {
        add: function () {
            dict_dialog.dialog('setTitle', '添加词条');
            dict_form.form('clear');
            if (menubelong == menu_list) {
                $("#dict_menu").hide();
                dict_dialog.css('height', 160);
            } else {
                $("#dict_menu").show();
                dict_dialog.css('height', 200);
            }
            dict_dialog.dialog('open');
        }
        ,
        edit: function () {
            var row = menubelong.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
            } else {
                dict_form.form('clear');
                if (menubelong == menu_list) {
                    $("#dict_menu").hide();
                    dict_dialog.css('height', 160);
                } else {
                    $("#dict_menu").show();
                    dict_dialog.css('height', 200);
                }
                dict_form.form('load', row);
                dict_dialog.dialog('setTitle', '编辑词条');
                dict_dialog.dialog('open');
            }
        },
        save: function () {
            dict_form.form('submit', {
                    url: '/dictionary/saveOrUpdate.do',
                    success: function (data) {
                        var res = $.parseJSON(data);
                        if (res.success) {
                            $.messager.alert('温馨提示', res.msg, 'info', function () {
                                dict_dialog.dialog('close');
                                menubelong.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', res.msg, 'error');
                        }
                    }
                }
            );
        },
        delete: function () {
            var row = menubelong.datagrid('getSelected');
            if (row == null) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'info');
            } else {
                var msg;
                if (menubelong == menu_list) {
                    $.messager.confirm('温馨提示', '本操作将删除分类下所有的词条,是否继续?', function (r) {
                            if (r) {
                                $.get('/dictionary/deleteMenu.do', {id: row.id}, function (data) {
                                    if (data.success) {
                                        $.messager.alert('温馨提示', data.msg, 'info', function () {
                                            dict_dialog.dialog('close');
                                            window.location.reload();
                                        });
                                    } else {
                                        $.messager.alert('温馨提示', data.msg, 'error');
                                    }
                                }, 'json')
                            }
                        }
                    )
                } else {
                    $.messager.confirm('温馨提示', '是否确认删除该词条?', function (r) {
                            if (r) {
                                $.get('/dictionary/deleteWord.do', {id: row.id}, function (data) {
                                    if (data.success) {
                                        $.messager.alert('温馨提示', data.msg, 'info', function () {
                                            dict_dialog.dialog('close');
                                            word_list.datalist('reload');
                                        });
                                    } else {
                                        $.messager.alert('温馨提示', data.msg, 'error');
                                    }
                                }, 'json')
                            }
                        }
                    )
                }
            }
        },
        cancel: function () {
            dict_dialog.dialog('close');
        }
        ,
        reload: function () {
            word_list.datagrid('reload');
            menu_list.datagrid('reload');
        }
    }
})
